package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    /**
     * 分类分页查询
     */
    @GetMapping("/page")
    public Result pageSelect(CategoryPageQueryDTO query) {
        PageResult pageResult = categoryService.pageSelect(query);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用分类
     * 修改分类状态
     */
    @PostMapping("/status/{status}")
    public Result updateStatusById(@PathVariable Integer status, Long id) {
        categoryService.updateStatusById(id);
        return Result.success();
    }

    /**
     * 新增分类
     */
    @PostMapping
    public Result insertCategory(@RequestBody Category category) {
        categoryService.insertCategory(category);
        return Result.success();
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    public Result deleteById(@RequestParam Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    public Result getType(Integer type){
        List<Category> category =categoryService.getType(type);
        return Result.success(category) ;
    }

}
